import {defineStore} from 'pinia'
import {ref} from "vue"
import {
    adminGetAdminkey,
    adminUpdateRota,
    adminProduceRota,
    adminGetUserTable,
    adminDeleteUserTable
} from '@/apis/AdminApi.js'
import {ElMessage} from 'element-plus';
import {publicKeysValidate} from "@/apis/PublicApi.js";

export const useAdminStore = defineStore('admin', () => {
        const adminKey = ref(null)
        const userKeyLink = ref(null)
        const keyList = ref([])

        const rota = ref(null)

        // 添加key
        const setKeyList = (key) => {
            if (!keyList.value.includes(key)) {
                keyList.value.push(key)
            }
        }

        // 获取密钥
        const getKey = (data, callback, finallyCb) => {
            adminGetAdminkey(data).then(res => {
                // console.log("密钥的信息为：", res.data);
                if (res.code === 1) {
                    adminKey.value = res.data.key
                    userKeyLink.value = res.data.userKeyLink
                    setKeyList(res.data.key)
                    callback()
                }
                // console.log(adminKey.value)
            }).finally(() => finallyCb && finallyCb());
        }

        // 生成值班表
        const produceRota = (key, callBack, finallyCb) => {
            adminProduceRota(key).then(res => {
                // console.log(res)
                if (res.code === 1) {
                    rota.value = res.data
                    callBack()
                }
            }).finally(() => finallyCb && finallyCb());
        }

        // 修改值班表
        const updateRotaStore = (data, callBack) => {
            const key = data.key
            const rota = data.rota
            rota[data.x][data.y] = data.names
            adminUpdateRota(key, rota).then(res => {
                if (res.code === 1) {
                    callBack()
                    ElMessage.success("修改成功")
                } else {
                    ElMessage.error("修改失败")
                }
            })
        }

        // 获取用户课程表
        const TimetableList = ref(null)
        const getUserTableStore = (userId, callBack) => {
            console.log("获取用户课程表", userId)
            adminGetUserTable(userId).then(res => {
                if (res.code === 1) {
                    TimetableList.value = res.data
                    callBack()
                }
            })
        }

        // 删除用户课程表
        const deleteUserTableStore = (userId, submitId, callBack) => {
            adminDeleteUserTable(userId, submitId).then(res => {
                if (res.code === 1) {
                    ElMessage.success("删除成功")
                    callBack()
                } else {
                    ElMessage.error("删除失败")
                }
            })
        }

        // 验证密钥的合法性
        const keyValidate = () => {
            const validateInfo = ref()
            if (keyList.value.length !== 0) {
                publicKeysValidate(keyList).then(res => {
                    validateInfo.value = res.data
                    for (let i = 0; i < validateInfo.value.length; i++) {
                        // 如果valid为false
                        if (!validateInfo.value[i].valid) {
                            // 从keys中移除对应的密钥
                            let index = keyList.value.indexOf(validateInfo.value[i].key);
                            if (index !== -1) {
                                keyList.value.splice(index, 1);
                            }
                        }
                    }
                });
            }
        }
        return {
            adminKey,
            userKeyLink,
            keyList,
            TimetableList,
            getKey,
            updateRotaStore,
            produceRota,
            getUserTableStore,
            deleteUserTableStore,
            setKeyList,
            keyValidate
        }
    },
    {
        persist: {
            storage: localStorage,
            paths: ['adminKey', 'userKeyLink', 'keyList'],
        }
    }
)